<template>
  <a-modal
    title="重命名文件"
    :width="450"
    :visible="visible"
    @ok="handleOk"
    @cancel="handleCancel"
  >


    <a-form-model ref="form" :model="fileInfo" :rules="validatorRules">
      <a-form-model-item :labelCol="{span:4}" :wrapperCol="{span:20}" style="margin-bottom:10px"
                         :validate-status="validate_status1"  prop="fileName">
        <a-tooltip placement="topLeft">
          <template slot="title">
            <span>请输入文件名称</span>
          </template>
          <a-avatar style="backgroundColor:#87d068" icon="plus"/>
        </a-tooltip>

          <a-input v-model="fileInfo.fileName" :class="{'valid-error':validate_status1=='error'}" placeholder="请输入文件名称"
                   style="margin-left:10px;width: 80%">

          </a-input>
      </a-form-model-item>


    </a-form-model>
  </a-modal>
</template>

<script>

import Vue from 'vue'
import { DepartRenameFile} from '@/api/file.js'
import {filenameSplit} from "@/views/filemanage/filters";

export default {
  name: 'DepartFileRename',

  data() {
    return {
      visible: false,
      validate_status1: "",
      fileInfo:{},
      validatorRules: {
        fileName:  [{required: true, message: '请输入文件名称'}],
      },

    }
  },
  computed: {


  },
  methods: {
    clear() {
      this.visible = false;
      this.validate_status1 = ''
    },
    show(record) {
      this.visible = true;
      this.fileInfo = Object.assign({}, record);
    },

    handleOk() {
      let filename=this.fileInfo.fileName;
      if (filename === null || filename === undefined || filename === '') {
        this.validate_status1 = 'error'
        return false;
      }
      if(!this.fileInfo.dir){

        let name = filenameSplit(filename);
        if (!name.filename) {
          this.$message.warning ("错误文件名");
          return false
        }
        this.fileInfo.oldFilename = this.fileInfo.filename
        this.fileInfo.oldExtension = this.fileInfo.extension
        this.fileInfo.fileName = name.filename;
        this.fileInfo.extension = name.extension;
      }else {
        this.fileInfo.oldFilename = this.fileInfo.fileName
        this.fileInfo.oldExtension = this.fileInfo.extension
        this.fileInfo.fileName = filename;
        this.fileInfo.extension = this.fileInfo.extension
      }

      this.confirmRenameFile(this.fileInfo);
    },
    // 文件重命名模态框 确定按钮
    confirmRenameFile(fileInfo) {
      var that=this;
      console.log(fileInfo)
      let data = {
        userFileId: fileInfo.id,
        fileName: fileInfo.fileName
      }
      if (!fileInfo.dir) {
        data.extension = fileInfo.extension
      }
      DepartRenameFile(data).then((res) => {
        if (res.success){
          that.$emit('ok')
        }else {
          that.$message.warning(res.message);
        }
      }).finally(() => {
        that.close();
      });
    },

    // 关闭
    handleCancel() {
      this.close();
    },
    close() {
      this.visible = false;
      this.$refs.form.resetFields();
    },
  }
}
</script>

<style scoped>

</style>